package cn.yzgy.yzgyoa.ucenter.service.impl;

import cn.yzgy.yzgyoa.ucenter.dto.UserDTO;
import cn.yzgy.yzgyoa.ucenter.entity.SysUser;
import cn.yzgy.yzgyoa.ucenter.mapper.SysUserMapper;
import cn.yzgy.yzgyoa.ucenter.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yqsen
 * @since 2019-08-20
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Override
    public SysUser getUserByUserName(String userName) {
        return baseMapper.selectOne(new QueryWrapper<SysUser>().eq("login_name", userName));
    }

    @Override
    public IPage<UserDTO> queryAllUserDTO(QueryWrapper<UserDTO> wrapper,IPage<UserDTO> ipage, Integer num, Integer size) {
        ipage.setRecords(baseMapper.queryAll(wrapper,num,size));
        return ipage;
    }

    @Override
    public String countData() {
        return baseMapper.countData()+"";
    }


    @Override
    public int check(SysUser sysUser) {
        int num=1;
        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name",sysUser.getLoginName());
        SysUser result=baseMapper.check(wrapper);
        if(result == null)
        {
            num=0;
        }
        return num;
    }

    @Override
    public int addSysUser(SysUser sysUser) {
        return baseMapper.insert(sysUser);
    }

    @Override
    public IPage<SysUser> queryAll(IPage<SysUser> ipage, QueryWrapper<SysUser> wrapper) {
        return baseMapper.selectPage(ipage,wrapper);
    }

    @Override
    public int delSysUser(List<String> result) {
        UpdateWrapper<SysUser> wrapper = new UpdateWrapper<>();
        wrapper.in(true,"uid",result);
        return baseMapper.delete(wrapper);
    }

    @Override
    public int resetPwd(String psw,String salt,String userId) {
        String[] result = userId.split(",");
        UpdateWrapper<SysUser> wrapper = new UpdateWrapper<>();
        int num=0;
        for (String s : result) {
            wrapper.eq("uid", s);
            num += baseMapper.change(psw,salt, wrapper);
        }
        return num;
    }

    @Override
    public int changeInfo(SysUser sysUser) {
        UpdateWrapper<SysUser> wrapper = new UpdateWrapper<>();
        wrapper.eq("uid", sysUser.getId());
        return baseMapper.update(sysUser,wrapper);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int updateData(String result, String way) {
        int num=0;
        String stop="stop";
        String start="start";
        UpdateWrapper<SysUser> wrapper = new UpdateWrapper<>();
        try{
            wrapper.eq("uid",Long.parseLong(result));
            if(stop.equals(way)){
                num=baseMapper.stopState(wrapper);
            }
            else if (start.equals(way)){
                num=baseMapper.startState(wrapper);
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return num;
    }
}
